7C - Line - CodeForces Solution


math number theory *1800

Please click on ads to support us..

Python Code:

def gcd(a, b):
    if a == 0:
        return 0, 1, b;

    x, y, g = gcd(b % a, a)
    return y - (b // a) * x, x, g

a, b, c = map(int, input().split())
c = -c
x, y, g = gcd(a, b)

if c % g != 0:
    print("-1")
else:
    x *= c // g
    y *= c // g
    print(x, " ", y)

C++ Code:

#include <bits/stdc++.h>
#define deb(x) cout<<endl<<#x <<" = "<<x<<endl<<endl
using namespace std;
typedef long long ll;
typedef double dd;

ll gcd(ll a,ll b,ll &x,ll &y)
{
    if(a==0){
        x=0;
        y=1;
        return b;
    }
    ll x1,y1;
    ll d=gcd(b%a,a,x1,y1);
    x=y1-(b/a)*x1;
    y=x1;
    return d;
}

bool diophantine(ll a,ll b,ll c,ll &x,ll &y)  // ax + by = c     the first answer in x and y
{
    ll x0,y0;
    ll d=gcd(a,b,x0,y0);
    if(c%d!=0) return false;
    x=x0*(c/d);
    y=y0*(c/d);
    return true;
}

int main()
{
    ll a,b,c; cin>>a>>b>>c;
    ll x,y;
    bool ok=diophantine(a,b,-c,x,y);
    if(ok) cout<<x<<' '<<y<<endl;
    else cout<<-1<<endl;
}


Comments

Submit
0 Comments
More Questions

433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa
202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1
363B - Fence
991B - Getting an A